package game;

import javax.microedition.lcdui.Display;
import javax.microedition.m2g.SVGAnimator;
import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;

import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGElement;

/**
 * Listener for the highscore-screen
 * @author Gustav
 * @since 29.5.2010
 */
class MIDPSVGEventListenerGameOver implements SVGEventListener {
    private SVGAnimator svgAnimator;
    private Document doc;
    int entered = 0;
    int x = 0, y = 0;

    /**
     * One-time initialization (load the data from the database)
     */
    public MIDPSVGEventListenerGameOver(SVGAnimator svgAnimator, SVGImage svgImage) {
        this.svgAnimator = svgAnimator;
        this.doc = svgImage.getDocument();
    }

    public void init(){
        SVGElement text = (SVGElement)doc.getElementById("name");
        text.setTrait("#text", "A");
        text = (SVGElement)doc.getElementById("score");
        text.setTrait("#text", ""+LUMM2_Game.arena.player.getScore());
    }

    /**
     * user pressed enter: go back to the menu
     */
    public void keyPressed(int keystate) {
        SVGElement text = (SVGElement)doc.getElementById("name");
        if(keystate == 50 || keystate == -1){
            String b = text.getTrait("#text");
            char c = b.charAt(b.length()-1);
            if(c == 'Z')
                c = 'A';
            else
                c += 1;
            b = b.substring(0, b.length()-1)+c;
            text.setTrait("#text", b);
        }
        if(keystate == 56 || keystate == -2){
            String b = text.getTrait("#text");
            char c = b.charAt(b.length()-1);
            if(c == 'A')
                c = 'Z';
            else
                c -= 1;
            b = b.substring(0, b.length()-1)+c;
            text.setTrait("#text", b);
        }
    	if(keystate == -5) { //enter, stops the menu and change canvas
            entered++;
            if(entered == 3){
                svgAnimator.stop();
                LUMM2_Midlet.rms.addScore(LUMM2_Game.arena.player.getScore(), text.getTrait("#text"));
    		LUMM2_Game.currentGameState = LUMM2_Game.MENU;
    		Display.getDisplay(LUMM2_Canvas.midlet).setCurrent(LUMM2_Midlet.pc);
                System.out.println("1");
            }
            else{
                text.setTrait("#text", text.getTrait("#text")+"A");
            }
    	}
    }

    /**
     * other methos I dont need... but had to implement
     */
    public void keyReleased(int k) { /* ... */ }
    public void hideNotify() { /* ... */ }
    public void pointerPressed(int a, int b) { /* ... */ }
    public void pointerReleased(int a, int b) { /* ... */ }
    public void showNotify() { /* ... */ }
    public void sizeChanged(int a, int b) { /* ... */ }
}
